/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oea.struts.actions;

import com.oea.hibernate.entities.Test;
import com.oea.struts.forms.AddTestForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author unnati
 */
public class AddTestAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static  String path;

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        AddTestForm atf=(AddTestForm)form;
        
        Test t= new Test();
        
        t.setName(atf.getName());
        t.setDuration(atf.getTdhour()+":"+atf.getTdminute()+":"+atf.getTdsecond());
        t.setTdate(atf.getDay()+"-"+atf.getMonth()+"-"+atf.getYear());
        t.setCutoffmarks(atf.getCutoffmarks());
        t.setTotalquestions(atf.getTotalquestions());
        t.setPositivemarking(atf.getPositivemarking());
        t.setNegativemarking(atf.getNegativemarking());
        t.setTtime(atf.getTthour()+":"+atf.getTtminute()+":"+atf.getTtsecond());
        t.setDescription(atf.getDescription());
        t.setAddedby(atf.getAddedby());
        t.setEid(atf.getEid());
        t.setSubid(atf.getSubid());
        t=(Test) t.add();
        
                
        if(t!=null)
        {
            List<Test> li_alltests=(List<Test>) t.getAll();
            request.getSession().setAttribute("li_alltests", li_alltests);
            request.getSession().setAttribute("page_li_alltests", li_alltests);
            
            double pagelink=Math.ceil((float)li_alltests.size()/10);
            request.getSession().setAttribute("pagelink", pagelink);
            for (Test test : li_alltests) 
            {
                test.setExam();
                test.setFaculty();
                test.setSubject();
            }
            
            path="view_test";  
        }
        
        
        return mapping.findForward(path);
    }
}
